/*
 * Created by JFormDesigner on Wed Jul 03 23:29:34 CST 2024
 */

package scct.ZhuZhouXing.ui;

import jdk.nashorn.internal.scripts.JO;
import scct.ZhuZhouXing.dao.StaffDao;
import scct.ZhuZhouXing.model.Product;
import scct.ZhuZhouXing.model.Staff;

import java.awt.*;
import java.awt.event.*;
import java.util.Iterator;
import java.util.List;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

/**
 * @author zr155
 */
public class PaneStaffManager extends JPanel {
    public static DefaultTableModel model;
    MainFrom main;
    public PaneStaffManager(MainFrom main) {
        this.main = main;
        initComponents();
        initView();//tab表提示文字
        getTable();//tab表显示数据库里面的数据
    }

    private void initView() {
        model = (DefaultTableModel)this.table1.getModel();
        model.addColumn("员工的id");
        model.addColumn("员工姓名");
        model.addColumn("员工性别");
        model.addColumn("员工年龄");
        model.addColumn("员工职位");
        model.addColumn("员工电话");
        model.addColumn("员工薪资");
        getTable();
    }
//调用dao包中
    public static void getTable() {
        model.setRowCount(0);//删除原有数据
        List<Staff> staff = StaffDao.getallstaff();
        for (Staff staffs : staff) {
            Object[] row = {staffs.getAid(),
                    staffs.getAusername(),
                    staffs.getGender(),
                    staffs.getAge(),
                    staffs.getPosition(),
                    staffs.getPhone(),
                    staffs.getSalary()};
            model.addRow(row);
        }
        model.fireTableDataChanged();//刷新界面显示数据
    }
    //========增加员工的监听============
    private void btnAdd(ActionEvent e) {
        DlgStaffAdd dlgStaffAdd =new DlgStaffAdd(main);
        dlgStaffAdd.setSize(800,600);
        dlgStaffAdd.setModal(true);
        dlgStaffAdd.setVisible(true);


    }

    private void btnUpdate(ActionEvent e) {
        if (table1.getSelectedRowCount()==0){
            JOptionPane.showMessageDialog(this,"请选择要修改的员工");
            return;
        }
        int index=table1.getSelectedRow();
        int id=Integer.parseInt(model.getValueAt(index,0).toString());
        DlgStaffUpdate dlgStaffUpdate =new DlgStaffUpdate(main,id);
        dlgStaffUpdate.setSize(800,600);
        dlgStaffUpdate.setModal(true);
        dlgStaffUpdate.setVisible(true);
    }

    private void btndelect(ActionEvent e) {
        if (table1.getSelectedRowCount()==0){
            JOptionPane.showMessageDialog(this,"请选择要删除的员工");
            return;
        }
        int index=table1.getSelectedRow();
        int id=Integer.parseInt(model.getValueAt(index,0).toString());
        StaffDao staffDao =new StaffDao();
        if (staffDao.deStaff(id)){
            JOptionPane.showMessageDialog(this,"删除成功！");
            getTable();
        }
        
    }

    private void btnReset(ActionEvent e) {
        getTable();
    }

    private void btnSelect(ActionEvent e) {
        model.setRowCount(0);
        String name=txtQuery.getText();
        if (name.isEmpty()){
            JOptionPane.showMessageDialog(this,"搜索框为空");
        }
        StaffDao staffDao=new StaffDao();
        List<Staff> staff =staffDao.getselect(name);
        for (Staff staffs : staff) {
            Object[] row = {staffs.getAid(),
                    staffs.getAusername(),
                    staffs.getGender(),
                    staffs.getAge(),
                    staffs.getPosition(),
                    staffs.getPhone(),
                    staffs.getSalary()};
            model.addRow(row);
        }
        model.fireTableDataChanged();//刷新界面显示数据
        
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - zhur
        panel1 = new JPanel();
        btnAdd = new JButton();
        btnUpdate = new JButton();
        btndelect = new JButton();
        scrollPane1 = new JScrollPane();
        table1 = new JTable();
        panel2 = new JPanel();
        comScreen = new JComboBox<>();
        label1 = new JLabel();
        txtQuery = new JTextField();
        btnSelect = new JButton();
        btnReset = new JButton();

        //======== this ========
        setBorder(new javax.swing.border.CompoundBorder(new javax.swing.border.TitledBorder(
        new javax.swing.border.EmptyBorder(0,0,0,0), "JFor\u006dDesi\u0067ner \u0045valu\u0061tion"
        ,javax.swing.border.TitledBorder.CENTER,javax.swing.border.TitledBorder.BOTTOM
        ,new Font("Dia\u006cog",Font.BOLD,12)
        ,Color.red), getBorder())); addPropertyChangeListener(
        new java.beans.PropertyChangeListener(){@Override public void propertyChange(java.beans.PropertyChangeEvent e
        ){if("bord\u0065r".equals(e.getPropertyName()))throw new RuntimeException()
        ;}});
        setLayout(new BorderLayout());

        //======== panel1 ========
        {
            panel1.setLayout(new FlowLayout(FlowLayout.RIGHT));

            //---- btnAdd ----
            btnAdd.setText("\u6dfb\u52a0\u5458\u5de5\u4fe1\u606f");
            btnAdd.addActionListener(e -> btnAdd(e));
            panel1.add(btnAdd);

            //---- btnUpdate ----
            btnUpdate.setText("\u4fee\u6539\u5458\u5de5\u4fe1\u606f");
            btnUpdate.addActionListener(e -> btnUpdate(e));
            panel1.add(btnUpdate);

            //---- btndelect ----
            btndelect.setText("\u5220\u9664\u5458\u5de5\u4fe1\u606f");
            btndelect.addActionListener(e -> btndelect(e));
            panel1.add(btndelect);
        }
        add(panel1, BorderLayout.NORTH);

        //======== scrollPane1 ========
        {
            scrollPane1.setViewportView(table1);
        }
        add(scrollPane1, BorderLayout.CENTER);

        //======== panel2 ========
        {
            panel2.setLayout(new BoxLayout(panel2, BoxLayout.X_AXIS));

            //---- comScreen ----
            comScreen.setModel(new DefaultComboBoxModel<>(new String[] {
                "\u5168\u90e8",
                "\u7537",
                "\u5973"
            }));
            panel2.add(comScreen);

            //---- label1 ----
            label1.setText("\u8bf7\u8f93\u5165\u5458\u5de5\u59d3\u540d\u8fdb\u884c\u67e5\u8be2");
            panel2.add(label1);
            panel2.add(txtQuery);

            //---- btnSelect ----
            btnSelect.setText("\u67e5\u8be2");
            btnSelect.addActionListener(e -> btnSelect(e));
            panel2.add(btnSelect);

            //---- btnReset ----
            btnReset.setText("\u91cd\u7f6e\u9875\u9762");
            btnReset.addActionListener(e -> btnReset(e));
            panel2.add(btnReset);
        }
        add(panel2, BorderLayout.SOUTH);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - zhur
    private JPanel panel1;
    private JButton btnAdd;
    private JButton btnUpdate;
    private JButton btndelect;
    private JScrollPane scrollPane1;
    private JTable table1;
    private JPanel panel2;
    private JComboBox<String> comScreen;
    private JLabel label1;
    private JTextField txtQuery;
    private JButton btnSelect;
    private JButton btnReset;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
